{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n————————————————\\n版权声明：本文为CSDN博主「AI_盲」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。\\n原文链接：https://blog.csdn.net/xwd18280820053/article/details/80060544\\n'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Dropout\n",
    "from keras.layers import GRU,Conv1D,Activation,GlobalAveragePooling1D,Flatten,TimeDistributed,Reshape\n",
    "import keras\n",
    "from keras import regularizers\n",
    "from keras.callbacks import EarlyStopping\n",
    "from sklearn.metrics import roc_auc_score\n",
    "# from sklearn.cross_validation import StratifiedKFold\n",
    "from keras import backend as K\n",
    "# import my_callbacks\n",
    "from keras.layers.normalization import BatchNormalization\n",
    "import keras.backend.tensorflow_backend as KTF\n",
    "'''\n",
    "————————————————\n",
    "版权声明：本文为CSDN博主「AI_盲」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。\n",
    "原文链接：https://blog.csdn.net/xwd18280820053/article/details/80060544\n",
    "'''\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv1d_52 (Conv1D)           (None, 10, 128)           1664      \n",
      "_________________________________________________________________\n",
      "batch_normalization_51 (Batc (None, 10, 128)           512       \n",
      "_________________________________________________________________\n",
      "activation_61 (Activation)   (None, 10, 128)           0         \n",
      "_________________________________________________________________\n",
      "conv1d_53 (Conv1D)           (None, 8, 256)            98560     \n",
      "_________________________________________________________________\n",
      "batch_normalization_52 (Batc (None, 8, 256)            1024      \n",
      "_________________________________________________________________\n",
      "activation_62 (Activation)   (None, 8, 256)            0         \n",
      "_________________________________________________________________\n",
      "conv1d_54 (Conv1D)           (None, 6, 128)            98432     \n",
      "_________________________________________________________________\n",
      "batch_normalization_53 (Batc (None, 6, 128)            512       \n",
      "_________________________________________________________________\n",
      "activation_63 (Activation)   (None, 6, 128)            0         \n",
      "_________________________________________________________________\n",
      "time_distributed_4 (TimeDist (None, 6, 1)              129       \n",
      "_________________________________________________________________\n",
      "global_average_pooling1d_16  (None, 1)                 0         \n",
      "_________________________________________________________________\n",
      "dropout_16 (Dropout)         (None, 1)                 0         \n",
      "_________________________________________________________________\n",
      "activation_64 (Activation)   (None, 1)                 0         \n",
      "=================================================================\n",
      "Total params: 200,833\n",
      "Trainable params: 199,809\n",
      "Non-trainable params: 1,024\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "method:one\n",
    "'''\n",
    "max_lenth = 10 #序列长度\n",
    "max_features = 4 #变量维度\n",
    "\n",
    "model = Sequential()\n",
    "model.add(Conv1D(128, 3, padding='same', input_shape=(max_lenth, max_features))) # padding = 'same' 输出与输入序列长度一样，如果去掉，换成valid就是10-3+1=8\n",
    "model.add(BatchNormalization())\n",
    "model.add(Activation('relu'))\n",
    "model.add(Conv1D(256, 3))# output shape = (None,10-3+1,256)\n",
    "model.add(BatchNormalization())\n",
    "model.add(Activation('relu'))\n",
    "model.add(Conv1D(128, 3))# output shape = (None,8-3+1,256)\n",
    "model.add(BatchNormalization())\n",
    "model.add(Activation('relu'))\n",
    "model.add(TimeDistributed(Dense(1)))\n",
    "model.add(GlobalAveragePooling1D())   #时序的时间维度上全局池化 比如 x(t1),x(t2),...x(tn)，把它们相加并且取平均，这样就进行了降维，一个序列变成了一个值\n",
    "model.add(Dropout(0.5))\n",
    "# model.add(Flatten())\n",
    "# model.add(Dense(1))\n",
    "# model.add(TimeDistributed(Dense(1)))放这儿会出现问题，TimeDistributed只对（None,sequence,output_dim）这样格式的数据起作用，对（None,output_dim）不起作用\n",
    "model.add(Activation('sigmoid'))\n",
    "model.summary()\n",
    " \n",
    "model.compile(loss='binary_crossentropy',\n",
    "              optimizer='adam',\n",
    "              metrics=['binary_crossentropy'])  \n",
    "# model.summary()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv1d_142 (Conv1D)          (None, 10, 128)           1664      \n",
      "_________________________________________________________________\n",
      "batch_normalization_140 (Bat (None, 10, 128)           512       \n",
      "_________________________________________________________________\n",
      "activation_166 (Activation)  (None, 10, 128)           0         \n",
      "_________________________________________________________________\n",
      "conv1d_143 (Conv1D)          (None, 8, 256)            98560     \n",
      "_________________________________________________________________\n",
      "batch_normalization_141 (Bat (None, 8, 256)            1024      \n",
      "_________________________________________________________________\n",
      "activation_167 (Activation)  (None, 8, 256)            0         \n",
      "_________________________________________________________________\n",
      "conv1d_144 (Conv1D)          (None, 6, 128)            98432     \n",
      "_________________________________________________________________\n",
      "batch_normalization_142 (Bat (None, 6, 128)            512       \n",
      "_________________________________________________________________\n",
      "activation_168 (Activation)  (None, 6, 128)            0         \n",
      "_________________________________________________________________\n",
      "aaa (GlobalAveragePooling1D) (None, 128)               0         \n",
      "_________________________________________________________________\n",
      "bbb (Reshape)                (None, 128, 1)            0         \n",
      "_________________________________________________________________\n",
      "dropout_42 (Dropout)         (None, 128, 1)            0         \n",
      "_________________________________________________________________\n",
      "flatten (Flatten)            (None, 128)               0         \n",
      "_________________________________________________________________\n",
      "final (Dense)                (None, 1)                 129       \n",
      "_________________________________________________________________\n",
      "activation_169 (Activation)  (None, 1)                 0         \n",
      "=================================================================\n",
      "Total params: 200,833\n",
      "Trainable params: 199,809\n",
      "Non-trainable params: 1,024\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "method:two\n",
    "'''\n",
    "max_lenth = 10 #序列长度\n",
    "max_features = 4 #变量维度\n",
    "\n",
    "model = Sequential()\n",
    "model.add(Conv1D(128, 3, padding='same', input_shape=(max_lenth, max_features))) # padding = 'same' 输出与输入序列长度一样，如果去掉，换成valid就是10-3+1=8\n",
    "model.add(BatchNormalization())\n",
    "model.add(Activation('relu'))\n",
    "model.add(Conv1D(256, 3))# output shape = (None,10-3+1,256)\n",
    "model.add(BatchNormalization())\n",
    "model.add(Activation('relu'))\n",
    "model.add(Conv1D(128, 3))# output shape = (None,8-3+1,256)\n",
    "model.add(BatchNormalization())\n",
    "model.add(Activation('relu'))\n",
    "# model.add(TimeDistributed(Dense(1)))\n",
    "model.add(GlobalAveragePooling1D(name='aaa'))   #时序的时间维度上全局池化 比如 x(t1),x(t2),...x(tn)，把它们相加并且取平均，这样就进行了降维，一个序列变成了一个值\n",
    "model.add(Reshape((128,1),name=\"bbb\"))#没调出来\n",
    "model.add(Dropout(0.5))\n",
    "model.add(Flatten(name='flatten'))\n",
    "# model.add(Flatten())\n",
    "model.add(Dense(1,name='final'))\n",
    "# model.add(TimeDistributed(Dense(1)))放这儿会出现问题，TimeDistributed只对（None,sequence,output_dim）这样格式的数据起作用，对（None,output_dim）不起作用\n",
    "model.add(Activation('sigmoid'))\n",
    "model.summary()\n",
    " \n",
    "model.compile(loss='binary_crossentropy',\n",
    "              optimizer='adam',\n",
    "              metrics=['binary_crossentropy'])  \n",
    "# model.summary()\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "ea70d47c1026074199160e48050b9f1be75d4ba7d6f2081b88238ca77789226b"
  },
  "kernelspec": {
   "display_name": "Python 3.7.11 ('pytorch-gpu')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
